<template>
  <div class="app-container">
    <!-- 个人信息 -->
    <el-form ref="fm" label-width="100px">
      <!-- 部门 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="部门">
            <el-input
              v-model="userInfo.departmentName"
              @focus="showDept = true"
            />
            <el-row v-show="showDept" class="selBox">
              <!-- 关闭按钮 -->
              <i class="el-icon-circle-close" @click="showDept = false" />
              <el-tree
                ref="tree"
                :data="departData"
                :props="{ label: 'name' }"
                @node-click="selDept"
              ></el-tree>
            </el-row>
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片  上传云服务器-->
            <UploadImg :staff-photo.sync="userInfo.staffPhoto" />
            <!-- <el-image :src="require('@/assets/common/head.jpg')"></el-image> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSaveUserDetailById">
            保存更新
          </el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { saveUserDetailById } from '@/api/employees'
import { mapActions } from 'vuex'
// 部门选择
import { getDepartments } from '@/api/departments'
import { transformTreeData } from '@/utils/index'
export default {
  props: {
    userInfo: {
      type: Object,
      default: () => ({})
    }
  },
  data () {
    return {
      // 控制部门选择显示隐藏
      showDept: false,
      // 部门数据
      departData: []
    }
  },
  created () {
    this.getDepartData()
  },
  methods: {
    // 选择部门
    selDept (currDept) {
      this.userInfo.departmentName = currDept.name
      // 选完关闭
      this.showDept = false
    },
    // 获取部门数据
    async getDepartData () {
      const { depts } = await getDepartments()
      this.departData = transformTreeData(depts)
    },
    ...mapActions('user', ['getUserInfoAction']),
    async hSaveUserDetailById () {
      // this.$refs.fm.validate(async flag => {
      //   if (!flag) return
      //   // 使用最新修改数据覆盖源数据 作为接口提交数据
      //   await saveUserDetailById(this.userInfo)
      //   this.$message.success('更新成功')
      // })
      try {
        await this.$refs.fm.validate()
        await saveUserDetailById(this.userInfo)
        // 更新右上角显示的登录人信息=》重新调用 vuex的action方法
        this.getUserInfoAction()
        this.$message.success('更新成功')
        this.$router.back()
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
